﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OrganizedIN.BAL.Common;
using System.Drawing;

namespace OrganizedIN.UI
{
    public partial class GroupShare : System.Web.UI.Page
    {
        OIDataContext dc = new OIDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string username = Session["UserID"].ToString();
                var udata = (from d in dc.UserInformations
                             where d.username == username
                             select d).SingleOrDefault();
                imgUser.ImageUrl = (udata.ProfilePicture == null || udata.ProfilePicture == "") ? "../../images/user-icon.jpg" : udata.ProfilePicture;
                lnkUser.Text = udata.FirstName + " " + udata.LastName;
                string html = "";
                if (Request["Photo_ID"] != null)
                {
                    var data = (from d in dc.User_Photos
                                join a in dc.user_photo_albums on d.album_id equals a.id into ps
                                from a in ps.DefaultIfEmpty()
                                where d.PhotoID == Convert.ToInt32(Request["Photo_ID"])
                                select new { d.Photo_Title, d.Photo_Detail, d.Photo_Event, d.Photo_Rating, d.Photo_Path, AlbumTitle = a.title }).SingleOrDefault();
                    if (data != null)
                    {
                        html += "<img style='float:left;' alt='" + data.Photo_Title + "' src='" + data.Photo_Path + "' width='100px' height='100px' /></a>";
                        html += "<h4>" + data.Photo_Title + "</h4>";
                        html += "<p>" + data.Photo_Detail + "</p>";
                        html += Common.createRatingImages(int.Parse(data.Photo_Rating.ToString()));
                        html += "<div style='clear:both;'></div>";
                    }
                }
                else if (Request["Video_ID"] != null)
                {
                    var data = (from d in dc.User_Videos
                                join a in dc.user_video_albums on d.album_id equals a.id into ps
                                from a in ps.DefaultIfEmpty()
                                where d.Video_Id == Convert.ToInt32(Request["Video_ID"])
                                select new { d.Video_Name, d.VideoDetail, d.VideoStatus, d.VideoRate, d.VideoPath, AlbumTitle = a.title }).SingleOrDefault();
                    if (data != null)
                    {
                        string imgPath = data.VideoPath.ToString();
                        imgPath = imgPath.Substring(0, imgPath.Length - 3) + "jpg";
                        html += "<img style='float:left;' alt='" + data.Video_Name + "' src='" + imgPath + "' width='100px' height='100px' /></a>";
                        html += "<h4>" + data.Video_Name + "</h4>";
                        html += "<p>" + data.VideoDetail + "</p>";
                        html += Common.createRatingImages(int.Parse(data.VideoRate.ToString()));
                        html += "<div style='clear:both;'></div>";
                    }
                }
                ltrlDetails.Text = html;
            }
        }

        protected void btnShare_Click(object sender, EventArgs e)
        {
            try
            {
                string username = Session["UserID"].ToString();
                tblGroupShare gs = new tblGroupShare();
                gs.Organizer_User_MappingID = (from d in dc.User_Organizer_Mappings where d.username == username select d.Organizer_User_MappingID).SingleOrDefault();
                gs.Remarks = txtComment.Text;
                gs.Group_ID = Convert.ToInt32(ddlGroup.SelectedValue);
                gs.ShareDate = DateTime.Now;
                if (Request["Photo_ID"] != null)
                {
                    gs.Photo_ID = Convert.ToInt32(Request["Photo_ID"]);
                }
                else if (Request["Video_ID"] != null)
                {
                    gs.Video_ID = Convert.ToInt32(Request["Video_ID"]);
                }
                else if (Request["Calender_ID"] != null)
                {
                    gs.Calender_ID = Request["Calender_ID"];
                }
                else if (Request["Todo_ID"] != null)
                {
                    gs.Todo_ID = Request["Todo_ID"];
                }
                else if (Request["Media_ID"] != null)
                {
                    gs.Media_ID = Request["Media_ID"];
                }
                else if (Request["Document_ID"] != null)
                {
                    gs.Document_ID = Request["Document_ID"];
                }
                dc.tblGroupShares.InsertOnSubmit(gs);
                dc.SubmitChanges();
                lblSuccess.ForeColor = Color.Green;
                lblSuccess.Text = "Your content has been shared successfully !";
                Timer1.Enabled = true;
                ClearForm();
            }
            catch (Exception ex)
            {
                lblSuccess.ForeColor = Color.Red;
                lblSuccess.Text = "Some error has occured ! Please try later !";
                Timer1.Enabled = true;
            }
        }

        private void ClearForm()
        {
            txtComment.Text = "";
            ddlGroup.SelectedIndex = 0;
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            lblSuccess.Visible = false;
            Timer1.Enabled = false;
        }
    }
}
